<?php

class SyncController extends Controller
{
	public $layout='synclog';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to access 'index' and 'view' actions.
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 */
	public function actionView()
	{
		$log=$this->loadModel();
		$this->render('view',array(
			'model'=>$log,
		));
	}


	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 */
	public function actionDelete()
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel()->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(array('index'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		if(Yii::app()->user->isGuest){
                    $this->redirect(array('/site/page', 'view'=>'about'));
		}
		$criteria=new CDbCriteria(array(
			'condition'=>'author_id='.Yii::app()->user->id,
			'order'=>'sync_time DESC',
		));

		$dataProvider=new CActiveDataProvider('SyncLog', array(
			'pagination'=>array(
				'pageSize'=>Yii::app()->params['logsPerPage'],
			),
			'criteria'=>$criteria,
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionMySyncs()
	{
		$criteria=new CDbCriteria(array(
			'condition'=>'author_id='.Yii::app()->user->id,
			'order'=>'sync_time DESC',
		));
		$dataProvider=new CActiveDataProvider('Post', array(
			'pagination'=>array(
				'pageSize'=>Yii::app()->params['logsPerPage'],
			),
			'criteria'=>$criteria,
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	/**
	 * Lists all models.
	 */
	public function actionAllSyncs()
	{
		$criteria=new CDbCriteria(array(
			'order'=>'sync_time DESC',
		));
		$dataProvider=new CActiveDataProvider('SyncLog', array(
			'pagination'=>array(
				'pageSize'=>Yii::app()->params['logsPerPage'],
			),
			'criteria'=>$criteria,
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new SyncLog('search');
		
		if(isset($_GET['SyncLog']))
			$model->attributes=$_GET['SyncLog'];
		$this->render('admin',array(
			'model'=>$model,
			'admin'=>false,
		));
	}
	/**
	 * Manages all models.
	 */
	public function actionSuperAdmin()
	{
		$model=new SyncLog('search');
		if(isset($_GET['SynLog']))
			$model->attributes=$_GET['SyncLog'];
		$this->render('admin',array(
			'model'=>$model,
		    'admin'=>true,
		));
	}
	


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
			{
				$condition='';
				$this->_model=SyncLog::model()->findByPk($_GET['id'], $condition);
			}
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}

	/**
	 * Creates a new comment.
	 * This method attempts to create a new comment based on the user input.
	 * If the comment is successfully created, the browser will be redirected
	 * to show the created comment.
	 * @param Post the post that the new comment belongs to
	 * @return Comment the comment instance
	 */
	public function actionTest(){
	 $post=new Post;
	 $post->id=1;
	 $post->title='aaaa';
	 $post->content='xxx';
	 echo $post->toJSON();
	}
}
